/*
 * WaitingFrame.java
 *
 * Created on 16 settembre 2008, 20.02
 */

package gui;

import com.sun.java.swing.plaf.windows.WindowsBorders.ProgressBarBorder;
import java.awt.event.*;

/**
 *
 * @author  dario
 */
public class WaitingDialog extends javax.swing.JDialog implements Runnable, ActionListener
{

    /** Creates new form WaitingFrame */
    public WaitingDialog(GuiDesktop gui) 
    {
        super(gui);
        initComponents();
        _gui = gui;
        cancelButton.addActionListener(this);
        //this.setModal(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel = new javax.swing.JPanel();
        progresBar = new javax.swing.JProgressBar();
        cancelButton = new javax.swing.JButton();

        setResizable(false);
        setUndecorated(true);

        panel.setBackground(new java.awt.Color(254, 254, 254));

        progresBar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        progresBar.setIndeterminate(true);

        cancelButton.setText("Cancel");

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelLayout.createSequentialGroup()
                .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(progresBar, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))
                    .addGroup(panelLayout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(progresBar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(cancelButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-270)/2, (screenSize.height-131)/2, 270, 131);
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new WaitingDialog(null).setVisible(true);
            }
        });
    }
    
    public void run()
    {
        this.setLocation(_gui.getX()+(_gui.getWidth()-this.getWidth())/2, _gui.getY()+(_gui.getHeight()-this.getHeight())/2);
        this.setVisible(true);
        _finished = false;
        setModal(true);
        progresBar.setStringPainted(true);
        while(!_finished)
        {
            try
            {
                progresBar.setString(_message);
                panel.paintImmediately(0, 0, panel.getWidth(), panel.getHeight());
                progresBar.paintImmediately(0, 0, progresBar.getWidth(), progresBar.getHeight());                
                Thread.sleep(2000);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        setModal(false);
        this.setVisible(false);
        System.out.println("Waiting thread finished correctly");
    }
    
    public void actionPerformed(ActionEvent e)
    {
        //E' stato premuto il pulsante annulla
        //Thread.currentThread().interrupt();
        System.out.println("User interrupt caugth");
        _gui.stopSynthesis();
        _finished = true;
    }
    
    public void setFinished(boolean b)
    {
        _finished = b;
    }
    
    public void setMessage(String msg)
    {
        _message = msg;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel panel;
    private javax.swing.JProgressBar progresBar;
    // End of variables declaration//GEN-END:variables
    private boolean _finished = false;
    private String _message = "";
    private GuiDesktop _gui;
}
